Skip to content

New article on extending widgets with the widget factory. #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

tjvantoll
Copy link
Member

No description provided.

@tjvantoll
Copy link
Member Author

Ping @jzaefferer and @scottgonzalez. If you guys have some time I would appreciate a read through of this.

This is the first part of implementing jquery/api.jqueryui.com#20.


### Creating Widget Extensions

Creating widgets with the widget factory is done by passing the name of the widget and a prototype object to `$.widget`. The following creats a "myWidget" widget in the "myNamespace" namespace.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"creats" -> "creates"

@jzaefferer
Copy link
Member

When talking about widget extensions we should also mention the lack of polymorphism on the plugin bridge. While you can extend dialog with a superDialog, you can't call dialog methods through the superDialog bridge. Once you have access to the instance itself, it'll work fine.

@tjvantoll
Copy link
Member Author

When talking about widget extensions we should also mention the lack of polymorphism on the plugin bridge. While you can extend dialog with a superDialog, you can't call dialog methods through the superDialog bridge. Once you have access to the instance itself, it'll work fine.

@jzaefferer To make sure I understand, this is what you're referring to correct?

$.widget( "my.superDialog", $.ui.dialog, {} );

var dialog = $( "<div></div>" ).superDialog();

// This works
dialog.superDialog( "close" );

// This doesn't
dialog.dialog( "close" );

@jzaefferer
Copy link
Member

Yep, exactly.

@tjvantoll
Copy link
Member Author

Thanks @jzaefferer. Updates made 878403c.


### Creating Widget Extensions

Creating widgets with the widget factory is done by passing the name of the widget and a prototype object to `$.widget`. The following creates a "myWidget" widget in the "myNamespace" namespace.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$.widget -> $.widget()

@scottgonzalez
Copy link
Member

We should probably build on top of the existing widget factory docs. Perhaps use custom.progressbar from http://learn.jquery.com/jquery-ui/widget-factory/how-to-use-the-widget-factory/ instead of ui.dialog as the starting point.


Here superDialog and dialog are essentially equivalent widgets with different names and namepaces. To make our new widget more interesting we can add methods to its prototype.

The prototype object to use for a widget is the final argument passed to `$.widget`. To this point our examples have been using an empty object. Let's add a method to the prototype:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$.widget -> $.widget()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"To this point" -> "Up to this point" or "So far"

@scottgonzalez
Copy link
Member

We should add a section about extending a widget instance for one-off customizations.

@tjvantoll
Copy link
Member Author

@scottgonzalez I addressed all of your comments other than:

We should probably build on top of the existing widget factory docs. Perhaps use custom.progressbar from http://learn.jquery.com/jquery-ui/widget-factory/how-to-use-the-widget-factory/ instead of ui.dialog as the starting point.

I started down this route and it became a little messy. These are progressbar's methods:

  • destroy
  • disable
  • enable
  • option
  • value
  • widget

I had trouble building good examples based off these methods because they're ones I don't think people generally use... and I really can't see anyone wanting to override them.

I think using dialog's open() and close() are just cleaner for this article. Also you don't have to mess with the appendTo( "body" ) line over and over again.

I did change the namespace to "custom" for consistency.

Feel free to ping me about this.


When we place methods on the prototype object, we are not actually overriding the original method - rather, we are placing a new method at a higher level in the prototype chain.

To make the parent's methods available, the widget factory provides two methods - `_super()` and `superApply()`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_superApply()


### Using `_super()` and `_superApply()` to Access Parents

The [`_super()`](http://api.jqueryui.com/jquery.widget/#method-_super) and [`_superApply()`](http://api.jqueryui.com/jquery.widget/#method-_superApply) are methods that access methods of the same same in the parent widget. Refer to the following example. Like the previous one, this example also overrides the `open()` method to log `"open"`. However, this time `_super()` is run to invoke dialog's `open()` and open the dialog.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either drop "The" or change to something like "The _super() and _superApply() methods access methods..."

@scottgonzalez
Copy link
Member

Just a few minor tweaks needed and that one sentence that I find really confusing.

@tjvantoll
Copy link
Member Author

Landed in 4cf8c56.

@tjvantoll tjvantoll closed this Sep 20, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants